$(document).ready(function() {
    var bDate;
    var eDate;
    init();
    function init() {
        allClazz();
    }
    $("#b_search").click(function() {
        bDate=$("#b_Date").val();
        eDate=$("#e_Date").val();
        initTable();
    });

    function initTable() {
        $("#table_show tr:not(:first)").empty();
        var userName;
        var eventName;
        var nums;
        var money;
        var clazzID = $("#selectClazz").val();
        $("#boolSearched").attr("disabled", "disabled");
        var clazz={
            id:clazzID
        }
        $.ajax({
            type: "POST",
            url: urlToUser+"/user/getUserByClazzId",
            data:clazz,
            dataType: "json",
            async: false,
            success: function (data1) {
                for(var i in data1) {
                    if(data1[i].type=="student"){
                        userName=data1[i].name;
                    }
                    else{
                        continue;
                    }
                   var userEvents={
                       userId:data1[i].id
                   }
                   $.ajax({
                       type:"get",
                       url:"/UserEvents/getUserEventsByUserId",
                       data:userEvents,
                       dataType:"json",
                       async: false,
                       success:function(data2) {
                           var eventIds=(data2.eventId).split(",");
                           for(var j in eventIds){
                               var searchDoEvent={
                                   userId:data1[i].id,
                                   eventId:eventIds[j],
                                   bDate:bDate,
                                   eDate:eDate
                               };
                               $.ajax({
                                   type:"get",
                                   url:"/doEvent/getNumsByUserIdAndEventId",
                                   data:searchDoEvent,
                                   dataType:"text",
                                   async: false,
                                   success:function(data) {
                                       nums=data;
                                   }
                               })
                               var event={
                                   id:eventIds[j]
                               }
                               $.ajax({
                                   type:"post",
                                   url:"/event/getEventById",
                                   data:event,
                                   dataType:"json",
                                   async: false,
                                   success:function(data) {
                                       eventName=data.name;
                                       money=nums*(data.money);
                                   }
                               })
                               $(document).attr("title",bDate+"--"+eDate+"打卡记录报表");
                               $("#table_show").append("<tr><td>"+bDate+"</td><td>"+eDate+"</td><td>"+userName+"</td><td>"+eventName+"</td><td>"+nums+"</td><td>"+money+"</td></tr>")
                           }
                       }
                   })
                }
                $('#table_show').DataTable( {
                    dom: 'Bfrtip',
                     retrieve: true,
                    "buttons": [
                             'excel','copy', 'print'
                    ]
                });
            }
        })
    }
    $("#b_toExcel").click(function() {
        $.ajax({
            type:"post",
            url:"/fileUtil/toExcel",
        })
    })

    function allClazz() {
        $.ajax({
            type: "GET",
            url: urlToUser+"/clazz/allClazz",
            dataType: "json",
            success: function (data) {
                if (data != null || data != "") {
                    var sClazz = $("#selectClazz");
                    for (var i in data) {
                        if(i==0){
                            sClazz.append("<option selected='selected' value=" + data[i].id + ">" + data[i].name + "</option>");
                        }
                        else {
                            sClazz.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                        }

                    }
                }
            }
        })
    }
});